package com.neuronbit.businessflow.runtime;

import com.neuronbit.businessflow.runtime.lifecycle.FlowLifecycleHook;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * The type Step.
 *
 * @param <T> the type parameter
 */
@Getter
public abstract class EndStep<T extends FlowContext> extends Step<T> {
    /**
     * The Log.
     */
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * Instantiates a new Step.
     *
     * @param id          the id
     * @param name        the name
     * @param description the description
     */
    public EndStep(String id, String name, String description, StepType type) {
        super(id, name, description, type);
    }

    /**
     * Call.
     *
     * @param context the context
     * @param flow    the flow
     * @throws Exception the exception
     */
    public abstract void call(T context, Flow flow) throws Exception;


    /**
     * Call.
     *
     * @param context the context
     * @param flow    the flow
     * @throws Exception the exception
     */
    public void end(T context, Flow flow) throws Exception {
        Collection<FlowLifecycleHook> flowHooks = flow.getStepBeanProvider().getFlowHooks();
        for (FlowLifecycleHook flowLifecycleHook : flowHooks) {
            if (flowLifecycleHook.support(context, flow)) {
                flowLifecycleHook.onEnd(context, flow);
            }
        }
        super.end(context, flow);
    }
}
